package com.quanquan.management.service;

import DTO.image.Image;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import utils.Request;
import utils.StringUtil;
import utils.StringUtils;

/**
 * @ClassName:ImageService
 * @Description:TODO
 * @Author:xiongjunbao
 * @Date:2019/6/12 13:52
 * @Version 1.0
 */
@Service
public class ImageService {

    @Value("${module-url.resourceUrl}")
    private String resourceUrl;

    public Image list() {
        String json = Request.postJson(resourceUrl + "image/query_right_and_interests", null);
        Image iamge = new Image();
        if (StringUtil.isEmpty(json)) {
            return iamge;
        }
        JSONObject jsonObject = JSON.parseObject(json);
        if (null == jsonObject) {
            return iamge;
        }
        Integer resCode = jsonObject.getInteger("resCode");
        if (0 == resCode) {
            Image data = jsonObject.getJSONObject("data").toJavaObject(Image.class);
            return data;
        }
        return iamge;
    }


    public Image findById() {
        String json = Request.postJson(resourceUrl + "image/query_right_and_interests", null);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        Image iamge = null;
        if (0 == resCode) {
            JSONObject data = jsonObject.getJSONObject("data");
            iamge = data.toJavaObject(Image.class);
        }
        return iamge;
    }

    public boolean update(String imageId) {
        if (StringUtils.isEmpty(imageId)) {
            return false;
        }
        Image image = new Image();
        image.setId(imageId);
        String json = Request.postJson(resourceUrl + "image/right_and_interests", image);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        if (0 == resCode) {
            return true;
        }
        return false;
    }




}
